package com.light.view;

import atlantafx.base.controls.ModalPane;
import atlantafx.base.theme.Styles;
import com.light.component.EditorPane;
import com.light.enums.Level;
import com.light.model.GitProject;
import com.light.thread.FxAsyncTask;
import com.light.util.FxApplicationContextUtils;
import com.light.util.H2PoolUtils;
import com.light.util.JGitUtils;
import com.light.util.NoticeUtils;
import javafx.collections.FXCollections;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.scene.text.Text;

import java.io.File;

public class DetailView extends StackPane {

    private final ComboBox<GitProject> choiceBox;
    private final Text describeLabel;
    private final Text remarkLabel;
    private final Text updateTimeText;
    private final Text createTimeText;
    private final Text authorText;
    private final Text remoteText;
    private final Text localText;
    private final ChoiceBox<String> branchChoice;
    private final ChoiceBox<Integer> levelChoice;

    public DetailView() {
        super();

        choiceBox = new ComboBox<>(FxApplicationContextUtils.GIT_PROJECT_OBSERVABLE_LIST);
        choiceBox.setMaxWidth(300);
        // 顶部
        var describeTitle = new Label("描 述: ");
        describeTitle.getStyleClass().add(Styles.TITLE_4);
        Tooltip tooltip = new Tooltip("双击编辑");
        describeTitle.setTooltip(tooltip);
        describeLabel = new Text();
        HBox describeBox = new HBox(10, describeTitle, describeLabel);

        describeLabel.setWrappingWidth(700);
        describeBox.setAlignment(Pos.TOP_LEFT);
        VBox topBox = new VBox(10, choiceBox, describeBox);

        // 中间
        // 布局GridPane
        GridPane gridPane = new GridPane();
        // 居中、组件水平距离和垂直距离
        gridPane.setAlignment(Pos.CENTER);
        gridPane.setHgap(5);
        gridPane.setVgap(10);

        // 给列设置样式
        ColumnConstraints columnConstraints = new ColumnConstraints();
        columnConstraints.setFillWidth(true);
        columnConstraints.setHgrow(Priority.ALWAYS);
        for (int i = 0; i < 4; i++) {
            gridPane.getColumnConstraints().add(columnConstraints);
        }

        // 给每行设置样式
        RowConstraints rowConstraints = new RowConstraints();
        rowConstraints.setFillHeight(true);
//        rowConstraints.setVgrow(Priority.ALWAYS);
        rowConstraints.setMinHeight(50d);
        for (int i = 0; i < 6; i++) {
            gridPane.getRowConstraints().add(rowConstraints);
        }

        Label authorTitle = new Label("仓库作者: ");
        authorTitle.getStyleClass().add(Styles.TITLE_4);
        authorText = new Text();
        gridPane.add(authorTitle, 0, 0);
        gridPane.add(authorText, 1, 0, 3, 1);

        Label remoteTitle = new Label("仓库地址: ");
        remoteTitle.getStyleClass().add(Styles.TITLE_4);
        remoteText = new Text();
        gridPane.add(remoteTitle, 0, 1);
        gridPane.add(remoteText, 1, 1, 3, 1);

        Label localTitle = new Label("本地地址: ");
        localTitle.getStyleClass().add(Styles.TITLE_4);
        localText = new Text();
        gridPane.add(localTitle, 0, 2);
        gridPane.add(localText, 1, 2, 3, 1);

        Label branchTitle = new Label("当前分支: ");
        branchTitle.getStyleClass().add(Styles.TITLE_4);
        branchChoice = new ChoiceBox<>();
        branchChoice.setMaxWidth(180d);
        gridPane.add(branchTitle, 0, 3);
        gridPane.add(branchChoice, 1, 3);

        Label levelTitle = new Label("学习等级: ");
        levelTitle.getStyleClass().add(Styles.TITLE_4);
        levelChoice = new ChoiceBox<>();
        levelChoice.getItems().addAll(0, 1, 2, 3, 4, 5);
        levelChoice.setMaxWidth(180d);
        gridPane.add(levelTitle, 2, 3);
        gridPane.add(levelChoice, 3, 3);

        Label createTimeTitle = new Label("创建时间: ");
        createTimeTitle.getStyleClass().add(Styles.TITLE_4);
        createTimeText = new Text();
        gridPane.add(createTimeTitle, 0, 4);
        gridPane.add(createTimeText, 1, 4);

        Label updateTimeTitle = new Label("最近更新: ");
        updateTimeTitle.getStyleClass().add(Styles.TITLE_4);
        updateTimeText = new Text();
        gridPane.add(updateTimeTitle, 2, 4);
        gridPane.add(updateTimeText, 3, 4);

        Label remarkTitle = new Label("备注: ");
        remarkTitle.setTooltip(tooltip);
        remarkTitle.getStyleClass().add(Styles.TITLE_4);
        remarkLabel = new Text();
        remarkLabel.setWrappingWidth(700);
        gridPane.add(remarkTitle, 0, 5);
        gridPane.add(remarkLabel, 1, 5, 3, 1);

        Pane centerPane = new Pane(gridPane);

        // 整体
        VBox vBox = new VBox(10, topBox, new Separator(), centerPane);
        vBox.setPadding(new Insets(5, 2, 5, 2));
        VBox.setVgrow(centerPane, Priority.ALWAYS);

        // 遮罩层
        ModalPane modalPane = new ModalPane();
        EditorPane editorPane = new EditorPane(modalPane, describeLabel, remarkLabel, updateTimeText);
        this.getChildren().addAll(modalPane, vBox);

        describeTitle.setOnMouseClicked(event -> {
            GitProject project = choiceBox.getValue();
            if (project == null) {
                NoticeUtils.show(null, "请选择项目", Level.WARN);
                return;
            }
            if (event.getClickCount() == 2) {
                editorPane.show(project, false);
            }
        });
        remarkTitle.setOnMouseClicked(event -> {
            GitProject project = choiceBox.getValue();
            if (project == null) {
                NoticeUtils.show(null, "请选择项目", Level.WARN);
                return;
            }
            if (event.getClickCount() == 2) {
                editorPane.show(project, true);
            }
        });

        choiceBox.getSelectionModel().selectedItemProperty().addListener((observable, oldValue, newValue) -> {
            if (null == newValue || oldValue == newValue) {
                return;
            }
            refresh(newValue);
        });

        levelChoice.getSelectionModel().selectedItemProperty().addListener((observable, oldValue, newValue) -> {
            GitProject gitProject = choiceBox.getValue();
            if (null == newValue || null == gitProject || newValue == gitProject.level().get()) {
                return;
            }
            // 更新入库
            gitProject.level().set(newValue);
            FxAsyncTask.runOnce(System.currentTimeMillis(), "更新项目学习等级 " + gitProject.name().get(),
                    () -> H2PoolUtils.updateGitProject(gitProject),
                    () -> updateTimeText.setText(gitProject.updateTime().get()));
        });
    }

    public void refresh(GitProject project) {
        choiceBox.getSelectionModel().select(project);

        describeLabel.setText(project.description().get());
        remarkLabel.setText(project.remark().get());
        updateTimeText.setText(project.updateTime().get());
        createTimeText.setText(project.createTime());
        authorText.setText(project.author().get());
        remoteText.setText(project.remote());
        localText.setText(project.local().get());

        // 异步获取所有分支
        branchChoice.getSelectionModel().clearSelection();
        FxAsyncTask.runOnceBack(System.currentTimeMillis(), "获取项目所有分支 " + project.name().get(),
                () -> JGitUtils.branchs(new File(project.local().get())),
                success -> {
                    if (success.isEmpty()) {
                        return;
                    }
                    branchChoice.setItems(FXCollections.observableList(success));
                    branchChoice.getSelectionModel().select(project.branch().get());
                },
                fail -> branchChoice.setItems(null));


        levelChoice.getSelectionModel().select(project.level().get());
    }
}
